package com.itheima.controller;

import com.itheima.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author zhangpei
 * @ClassName comminController
 * @date 2022/5/29/15:14
 */
@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {

    @PostMapping("/upload")
    public R<Object> upload(String type, HttpServletRequest request, MultipartFile file) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String s = simpleDateFormat.format(date);
        UUID uuid = UUID.randomUUID();
        String name = file.getOriginalFilename();
        String suffix = name.substring(name.lastIndexOf(".") + 1);
        String mime = type + "/" + suffix;
        long size = file.getSize();
        String fielName = uuid + "." + suffix;

        //判断路径是否存在
        String savePath = "upload/" + type + "/" + s + "/";
        //判断target下的路径是否存在
        String targetPath = this.getClass().getResource("/").getPath().substring(1);
        File savep1 = new File(targetPath + savePath);
        if (!savep1.exists()) {
            savep1.mkdirs();
        }
        //判断resource下的路径是否存在
        String resourcePath = targetPath;
        for (int i = 0; i < 3; i++) {
            int index = resourcePath.lastIndexOf("/");
            resourcePath = resourcePath.substring(0, index);
        }
        resourcePath = resourcePath + "/src/main/resources/";
        File savep2 = new File(resourcePath + savePath);
        if (!savep2.exists()) {
            savep2.mkdirs();
        }
        //文件保存路径及文件名
        savePath = savePath + fielName;
        HashMap<Object, Object> result = new HashMap<>();
        result.put("mime", mime);
        result.put("name", name);
        result.put("savePath", savePath);
        result.put("size", size);

        //保存到resource目录下的update中
        File save1 = new File(resourcePath + savePath);
        try {
            file.transferTo(save1);//保存一次之后就没了0.0
        } catch (IOException e) {
            e.printStackTrace();
        }
        //保存到target目录下的update中
        File save2 = new File(targetPath + savePath);//不能使用相对路径.否则底层会加上tomocat运行的绝对路径
    /*try {
        file.transferTo(save2);
    } catch (IOException e) {
        e.printStackTrace();
    }*/
        //resource目录下的update中的新增图片保存到target目录下
        try {
            IOUtils.copy(new FileInputStream(save1), new FileOutputStream(save2));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.success(result);
    }
}
